package com.baosight.risk.web.jlta;

import com.baosight.risk.api.jlta.JltaRebackService;
import com.baosight.risk.api.jlta.JltaTeamService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.jlta.entity.JltaReback;
import com.baosight.risk.service.jlta.entity.JltaTeam;
import com.baosight.risk.service.jlta.entity.JltaTeamUser;
import com.baosight.risk.service.jlta.vo.JltaTeamUserVO;
import com.baosight.risk.service.jlta.vo.JltaTeamVO;
import com.baosight.risk.service.jlta.vo.MutiTeamVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月03日
 * 描述： JltaTeamController
 * 版本: v1.0
 */
@Api(value = "我的团队", description = "我的团队", produces = MediaType.APPLICATION_JSON)
@Slf4j
@RestController
@RequestMapping("/jlta/team")
public class JltaTeamController extends BaseController {

    @Autowired
    private JltaTeamService jltaTeamService;
    @Autowired
    private JltaRebackService rebackService;


    @ApiOperation("获取我的团队信息列表")
    @GetMapping("ownerTeamList")
    public AjaxResult ownerTeamList() {
        XtzyUser sysUser = getUser();
        String owner = sysUser.getId();
        AjaxResult result = AjaxResult.success();
        result.put("data", jltaTeamService.selectOwnerTeam(owner));
        return result;
    }

    @ApiOperation("获取加入团队信息列表")
    @GetMapping("joinTeamList")
    public AjaxResult joinTeamList(JltaTeam jltaTeam) {
        XtzyUser sysUser = getUser();
        String userCode = sysUser.getUserCode();
        AjaxResult result = AjaxResult.success();
        result.put("data", jltaTeamService.selectJoinTeam(jltaTeam, userCode));
        return result;
    }

    @ApiOperation("新增团队")
    @PostMapping("addTeam")
    public AjaxResult addTeam(@RequestBody JltaTeamVO vo) {
        XtzyUser sysUser = getUser();
        String teamId = UUID.randomUUID().toString();
        JltaTeam jltaTeam = new JltaTeam();
        jltaTeam.setId(teamId);
        jltaTeam.setOwner(sysUser.getId());
        jltaTeam.setCreateBy(sysUser.getUserDesc());
        jltaTeam.setCreateTime(new Date());
        jltaTeam.setTeamName(vo.getTeamName());
        jltaTeam.setStatus("0");

        Integer result = jltaTeamService.addTeam(jltaTeam);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("根据名称搜索团队")
    @GetMapping("searchTeam")
    public AjaxResult searchTeam(@RequestParam("teamName") String teamName) {

        List<JltaTeam> jltaTeams = jltaTeamService.searchTeam(teamName);

        return AjaxResult.success("成功", jltaTeams);

    }

    @ApiOperation("修改团队")
    @PostMapping("editTeam")
    public AjaxResult editTeam(@RequestBody JltaTeamVO vo) {
        XtzyUser sysUser = getUser();
        String userCode = sysUser.getUserCode();
        JltaTeam jltaTeam = new JltaTeam();
        jltaTeam.setId(vo.getId());
        jltaTeam.setTeamName(vo.getTeamName());
        jltaTeam.setUpdateBy(userCode);
        jltaTeam.setUpdateTime(new Date());
        Integer result = jltaTeamService.editTeam(jltaTeam);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("删除团队")
    @PutMapping("deleteTeam")
    public AjaxResult deleteTeam(@RequestBody String id) {
        XtzyUser sysUser = getUser();
        String userCode = sysUser.getUserCode();
        Integer result = jltaTeamService.deleteTeam(id);
        if (result > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    @ApiOperation("获取团队成员列表")
    @GetMapping("teamUserList")
    public AjaxResult userList(JltaTeamUser jltaTeamUser) {
        startPage();
        return getResultPage(jltaTeamService.selectTeamUserList(jltaTeamUser));
    }


    @ApiOperation("新增团队成员")
    @PostMapping("addUser")
    public AjaxResult addUser(@RequestBody MutiTeamVO mutiTeamVO) {
        XtzyUser sysUser = getUser();
        String userCode = sysUser.getUserCode();
        String teamId = mutiTeamVO.getTeamId();
        List<JltaTeamUserVO> userList = mutiTeamVO.getTeamUserVOList();
        //判断权限
        String role = getRole(sysUser, teamId);
        if ("0".equals(role)) {
            return error("只有团队创建人和项目负责人可以新增成员！");
        }
        if (CollectionUtils.isNotEmpty(userList)) {
            List<JltaTeamUser> jltaTeamUsers = jltaTeamService.selectUserList(teamId);
            List<String> collect = jltaTeamUsers.stream().map(JltaTeamUser::getUserCode).collect(Collectors.toList());
            for (JltaTeamUserVO vo : userList) {
                //判断是否存在成员的标识   去重添加
                boolean flag = false;
                for (String c : collect) {
                    if (c.equals(vo.getUserCode())) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    continue;
                }
                JltaTeamUser jltaTeamUser = new JltaTeamUser();
                BeanUtils.copyBeanProp(jltaTeamUser, vo);
                jltaTeamUser.setId(UUID.randomUUID().toString());
                jltaTeamUser.setCreateBy(userCode);
                jltaTeamUser.setCreateTime(new Date());
                jltaTeamUser.setStatus("0");
                jltaTeamUser.setRole("0");
                jltaTeamUser.setRoleName("团队成员");
                jltaTeamService.addTeamUser(jltaTeamUser);
            }
        }
        return AjaxResult.success();
    }

    @ApiOperation("删除团队成员")
    @PutMapping("deleteUser")
    public AjaxResult deleteUser(@RequestBody MutiTeamVO mutiTeamVO) {
        XtzyUser sysUser = getUser();
        String userCode = sysUser.getUserCode();
        String teamId = mutiTeamVO.getTeamId();
        List<String> ids = mutiTeamVO.getIds();
        String role = getRole(sysUser, teamId);
        if ("0".equals(role)) {
            return error("只有团队创建人和项目负责人可以删除成员");
        }
        for (String id : ids) {
            JltaTeamUser jltaTeamUserQ = new JltaTeamUser();
            jltaTeamUserQ.setId(id);
            JltaTeamUser jltaTeamUser = jltaTeamService.selectOne(jltaTeamUserQ);
            //查询当前成员下是否有反馈任务
            JltaReback rebackQ = new JltaReback();
            rebackQ.setTeamId(teamId);
            rebackQ.setUserCode(jltaTeamUser.getUserCode());
            List<JltaReback> select = rebackService.select(rebackQ);
            if (CollectionUtils.isNotEmpty(select)) {
                     return  error("请检查团队成员下的已分配的任务！");
            }
            if (!"0".equals(jltaTeamUser.getRole()))
                continue;
            jltaTeamUser.setStatus("1");
            jltaTeamUser.setUpdateBy(userCode);
            jltaTeamUser.setUpdateTime(new Date());
            jltaTeamService.deleteTeamUser(jltaTeamUser);

        }
        return AjaxResult.success();

    }

    @ApiOperation("设置团队管理员")
    @GetMapping("teamAdmin")
    public AjaxResult teamAdmin(@RequestParam("userId") String userId) {
        Integer integer = jltaTeamService.teamAdmin(userId);
        return toAjax(integer);
    }

    @ApiOperation("取消团队管理员")
    @GetMapping("cancelTeamAdmin")
    public AjaxResult cancelTeamAdmin(@RequestParam("userId") String userId) {
        Integer integer = jltaTeamService.cancelTeamAdmin(userId);
        return toAjax(integer);
    }

    /**
     * 获得团队角色
     */
    private String getRole(XtzyUser sysUser, String teamId) {
        JltaTeamUser jltaTeamUserQ = new JltaTeamUser();
        jltaTeamUserQ.setUserCode(sysUser.getUserCode());
        jltaTeamUserQ.setTeamId(teamId);
        JltaTeamUser jltaTeamUser1 = jltaTeamService.selectOne(jltaTeamUserQ);
        String role = jltaTeamUser1.getRole();
        return role;

    }
}
